<?php
class Usermanager extends CI_Model {

	public function __construct()
	{
		$this->load->model('Util');
		$this->load->model('SessionManager');
		$this->load->model('DatabaseManager');
		//$this->load->database();
	}
	public function update_user($username,$sessionId)
	{
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$row=$this->DatabaseManager->get_user_info($username);
		return	$this->Util->format_return(1,1,'success',$this->get_user_callback($row));
	}

	public function get_userinfo_by_username($user,$username,$sessionId)
	{
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$row=$this->DatabaseManager->get_user_info($user);
		return	$this->Util->format_return(1,1,'success',$this->get_other_user_callback($row));
	}
	public function get_integral_detail($username,$sessionId){
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$userInfo=$this->DatabaseManager->get_user_info($username);
		$row=$this->DatabaseManager->get_integral_detail($username);
		$data=array('integral'=>$userInfo['integral']+0,'item'=>$row);
		return	$this->Util->format_return(1,1,'success',$data);
	}
	public function sign_in($username,$sessionId,$time)
	{
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$row=$this->DatabaseManager->get_user_info($username);
		$signInDate=$row['signInDate'];
		$moringDate=$this->Util->get_times_moring();
		$nextMoringDate=$this->Util->get_tomorrow_moring();
		$yesterdayMoringDate=$this->Util->get_yesterday_moring();
		if($signInDate>$moringDate&&$signInDate<$nextMoringDate){
			return $this->Util->format_return(0,9,'signIn error');
		}else{
			$signInCount=$row['signInCount'];
			$coin=$row['integral'];
			if($signInDate>$yesterdayMoringDate&&$signInDate<$nextMoringDate){
				$signInCount+=1;
			}else{
				$signInCount=1;
			}
			if($signInCount==1){
				$integral=1;
			}else if($signInCount==2){
				$integral=2;
			}else if ($signInCount==3){
				$integral=3;
			}else if ($signInCount>3){
				$integral=5;
			}
			$this->DatabaseManager->add_integral_detail($username,"签到奖励",0,$integral,$time);
			$this->DatabaseManager->sign_in($username,$signInCount,$time,$integral+$coin);
			return	$this->Util->format_return(1,1,'success',array('signInCount'=>$signInCount,'signInDate'=>$time,'integral'=>$integral+$coin));
		}
	}
	public function check_phone($phone){
		$userInfo=$this->DatabaseManager->get_user_info_by_phone($phone);
		if(count($userInfo)==0){
			return	$this->Util->format_return(1,1,'success',1);
		}else{
			return	$this->Util->format_return(0,13,'phone exist',13);
		}
	}
	public function add_integral_detail($username,$sessionId){
		$userInfo=$this->DatabaseManager->get_user_info($username);
		$row=$this->DatabaseManager->get_integral_detail($username);
		$data=array('integral'=>$userInfo['integral']+0,'item'=>$row);
		return	$this->Util->format_return(1,1,'success',$data);
	}
	public function add_friend($username,$sessionId,$friendId){
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		if(!$this->DatabaseManager->is_friend($username,$friendId)){
			$this->DatabaseManager->set_friend($username,$friendId);
		}
	}
	public function get_friends_list($username,$sessionId){
		if(!$this->SessionManager->check($username,$sessionId)){
			return $this->Util->format_return(0,6,'wrong session');
		}
		$data=$this->DatabaseManager->get_friends_list($username);
		return	$this->Util->format_return(1,1,'success',$data);
	}
	public function set_user_callback($row){
		$data=array(
		'username'=>$row['username'],
		'name'=>$row['name'],
		'phone'=>$row['phone'],
		'avatarUrl'=>$row['avatarUrl'],
		'integral'=>$row['integral'],
		'integralDetail'=>$row['integralDetail'],
		'releaseCount'=>$row['releaseCount'],
		'receiveCount'=>$row['receiveCount'],
		'level'=>$row['level'],
		'signInCount'=>$row['signInCount'],
		'sex'=>$row['sex'],
		'token'=>$row['token'],
		'sessionId'=>$this->SessionManager->set($row['username'],$row['password'])
		);
		return $data;
	}

	public function get_user_callback($row){
		$data=array(
		'username'=>$row['username'],
		'name'=>$row['name'],
		'phone'=>$row['phone'],
		'avatarUrl'=>$row['avatarUrl'],
		'integral'=>$row['integral']+0,
		'integralDetail'=>$row['integralDetail'],
		'releaseCount'=>$row['releaseCount']+0,
		'receiveCount'=>$row['receiveCount']+0,
		'level'=>$row['level']+0,
		'signInCount'=>$row['signInCount']+0,
		'signInDate'=>$row['signInDate']+0,
		'sex'=>$row['sex']+0,
		'token'=>$row['token'],
		'sessionId'=>$this->SessionManager->get($row['username'])
		);
		return $data;
	}
	public function get_other_user_callback($row){
		$data=array(
		'username'=>$row['username'],
		'name'=>$row['name'],
		'avatarUrl'=>$row['avatarUrl'],
		'sex'=>$row['sex']
		);
		return $data;
	}
}